package com.kdtech.analyse.news.ulinix;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * Qimbulak
 * @author Persh
 *
 */
public class QimbulakNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.Qimbulak.[a-z]*/info.asp\\?id=[0-9]*&tpg=infovis.asp",
		"http://www.qimbulak.[a-z]*/info.asp\\?id=[0-9]*&tpg=infovis.asp",
		"http://www.qimbulak.cn/tvis.asp\\?id=[0-9]*",
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		Element first=doc.select("div.kokramka div").first();
		if(first!=null){
			title=first.text();
		}
		if(StringUtils.isBlank(title)){
			first = doc.select("div[dir=rtl]").first();
			if(first!=null){
				title = first.text();
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.kokramka div[dir=rtl]"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		}
		date=DateUtils.matchDate(doc.select("font[dir=rtl]").text());

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	

	

	
}
